﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EmployeeAddControl.ascx.cs"  Inherits="Legazpi.Payroll.Web.Usercontrols.EmployeeAddControl" %>
    <script type="text/javascript">
        var serviceUrl = '<%=ResolveUrl("~/Webservices/EmployeeWebService.asmx")%>';
        var employeeViewUrl = '<%=ResolveUrl("~/Admin/EmployeeView.aspx")%>';
        var positionDialogUrl = '<%=ResolveUrl("~/PopupBoxes/PositionDialogBox.aspx")%>';
        var employeeListUrl = '<%=ResolveUrl("~/Admin/Employees.aspx")%>';
        var cashAdvDialogUrl = '<%=ResolveUrl("~/PopupBoxes/EmployeeCashAdvancesDialogBox.aspx")%>';
        var empAdjustDialogUrl = '<%=ResolveUrl("~/PopupBoxes/EmployeeAdjustmentsDialogBox.aspx")%>';

        $(document).ready(function () {
            Employee.PositionDropdownControlId = '<%=PositionDropdown.ClientID %>';
            Employee.ClientDropdownControlId = '<%=DeployedDropdown.ClientID %>';
            Employee.PositionPopUpDialogBox = positionDialogUrl;
            Employee.EmployeeViewUrl = employeeViewUrl;
            Employee.CashAdvanceViewUrl = cashAdvDialogUrl;
            Employee.AdjusmentsViewUrl = empAdjustDialogUrl;
            Employee.EmployeeId = <%=Model.Id %>;

            Employee.FullName = '<%=string.Format("{0}, {1} {2}",Model.LastName, Model.FirstName,Model.MiddleName) %>';
            $('#BirthDay').datepicker({
                onSelect: function (dateText, inst) {
                    $("#DateOfBirth").val(dateText);
                }
            });

            $('#EmployeeSubmitButton').click(function () {

                if (!$("#mainForm").valid()) {
                    return false;
                }

                var EmployeeObj = {
                    Id: $('#Id').val(),
                    FirstName: $('#FirstName').val(),
                    MiddleName: $('#MiddleName').val(),
                    LastName: $('#LastName').val(),
                    NickName: $('#NickName').val(),
                    SSS: $('#SSSno').val(),
                    Tin: $('#TINno').val(),
                    PhilHealth: $('#PHno').val(),
                    PagIbig: $('#Pagibigno').val(),
                    PrimarySchoolName: $('#School1').val(),
                    PrimarySchoolPeriod: $('#SchoolPeriod1').val(),
                    SecondarySchoolName: $('#School2').val(),
                    SecondarySchoolPeriod: $('#SchoolPeriod2').val(),
                    TertiarySchoolName: $('#School3').val(),
                    TertiarySchoolPeriod: $('#SchoolPeriod3').val(),
                    CollegeDegree: $('#CollegeDegree').val(),
                    PositionId: $('#<%=PositionDropdown.ClientID %> option:selected').val(),
                    DetachmentId: $('#<%=DeployedDropdown.ClientID %> option:selected').val(),
                    SupervisorId: $('#<%=SupervisorDropdown.ClientID %> option:selected').val(),
                    EmployedStatus: $('#<%=EmployedStatusDropDown.ClientID %> option:selected').val(),
                    DateOfBirth: $('#DateOfBirth').val(),
                    PlaceOfBirth: $('#PlaceOfBirth').val(),
                    Religion: $('#Religion').val(),
                    Qualifier: $('#Qualifier').val(),
                    Height: $('#Height').val(),
                    Weight: $('#Weight').val(),
                    Gender: $('#Gender option:selected').val(),
                    CivilStatus: $('#CivilStatus option:selected').val(),
                    YearsMarried: $('#YearsMarried').val(),
                    ATMno: $('#ATMno').val()

                };
                var json = JSON.stringify({ employeeItem: EmployeeObj });
                BlockUI.BlockElementLoading("page", "Saving...", true, 4);
                $.ajax({
                    type: "POST",
                    url: serviceUrl + "/AddEmployee",
                    data: json,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var response = eval('(' + data.d + ')');
                        if (response.Success) {
                            if (response.IsAdded)
                                PopUpDialog.MessageDialog("Success", 150, 100, true, "<center>Successfully Saved!</center>", $(this).attr("id"));
                            else
                                PopUpDialog.MessageDialog("Success", 150, 100, true, "<center>Successfully Updated!</center>", $(this).attr("id"));

                            $('#Id').val(response.EmployeeItem.Id);
                            $('#EmployeeSubmitButton').val('Update');
                            $('#employeeCode').html('Employee Code: <strong>' + response.EmployeeItem.EmployeeCode + '</strong>');
                        }
                    },
                    complete: function () {
                        BlockUI.UnBlockElement("page");
                    }
                });
            });

            $("#btnClear").click(function (e) {
                e.preventDefault();
                if (confirm("Clear form?")) {
                    $('#Id').val('0');
                    $(this).closest('form').find("input[type=text], textarea").val("");
                    $('#EmployeeSubmitButton').val('Submit');
                    $('#employeeCode').html('Employee Code: <strong></strong>');
                    $('#EmployeeCashAdv').hide();
                    $('#heading').html('New Employee');
                    
                }
            });

            $.validator.addMethod('greaterThanZero', function (value, element) {
                return (value > 0);
            }, 'Please select an option.');

            $("#mainForm").validate({
                rules: {
                    LastName: "required",
                    FirstName: "required",
                    MiddleName: "required",
                    <%=PositionDropdown.UniqueID %>: {
                        greaterThanZero : true
                    }
                },
                messages: {
                    LastName: "Please enter lastname",
                    FirstName: "Please enter firstname",
                    MiddleName: "Please enter middlename",
                    <%=PositionDropdown.UniqueID %>: "select position"
                }
            });

             $("#<%=btnBack.ClientID %>").click(function () {                
                 window.location = employeeListUrl;
             });


            $('#Search').autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: serviceUrl + "/FetchEmployees",
                        data: "{ 'employeeName': '" + request.term + "' }",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function(data) { return data; },
                        success: function(data) {
                            response($.map(data.d, function(item) {
                                return {
                                    label: item.EmployeeName,
                                    value: item.EmployeeName,
                                    id: item.Id
                                }
                            }))
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                },
                minLength: 2,
			    select: function( event, ui ) {
                    var id = ui.item.id;
                    var employeeName = ui.item.label;                    
                    window.location = employeeViewUrl + '?id=' + id;
			    }
            });

        });
    </script>             
        <h2 id="heading">
         <%if (Model.Id > 0){%>   
            <%=string.Format("{0}, {1} {2}",Model.LastName, Model.FirstName,Model.MiddleName) %>
         <%}else{ %>             
            New Employee
         <%} %>
        </h2>
        <p>
            Use the form below to create a new 
            employee.
        </p>
        
        Search : <input type ="text" id="Search"  value="" /> 
         <%if (Model.Id > 0) {%>   
            &nbsp;&nbsp;       
            <a href="#" id="EmployeeCashAdv" data-id="<%=Model.Id %>">Cash Advances</a> |
            <a href="#" id="EmployeeAdjusments" data-id="<%=Model.Id %>">Payroll Adjustments</a> 
         <%} %>         
         <hr />       
         <h3 id="employeeCode">Employee Code: <strong><%=Model.EmployeeCode %></strong></h3>   
            
        <div class="employeeInfo">
            <input type="hidden" id="Id" value="<%=Model.Id %>" />    
            <fieldset class="registerEmployee">
                <legend>Employee Information</legend>
                <p>                    
                    <label for="PositionDropdown">Position:  <a href="#" id="btnAddPosition">[ + ]</a></label>
                    <asp:DropDownList ID="PositionDropdown" runat="server" name="PositionDropdown" AppendDataBoundItems="true">
                        <asp:ListItem Text="--Select--" Value="0" /> 
                    </asp:DropDownList>
                                                            
                </p>
                <p>
                    <label for="DeployedDropdown">Deployed At:</label>                    
                    <asp:DropDownList ID="DeployedDropdown" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Text="--Select--" Value="0" /> 
                    </asp:DropDownList>                    
                    
                </p>
                <p>                    
                    <label for="ProjectTitleDropdown">Project Title:</label>
                    <asp:DropDownList ID="ProjectTitleDropdown" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Text="--Select One--" Value="0" /> 
                    </asp:DropDownList>
                </p>
                <p>
                    <label for="SupervisorDropdown">Supervisor:</label>                    
                    <asp:DropDownList ID="SupervisorDropdown" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Text="--Select--" Value="0" /> 
                    </asp:DropDownList>
                </p>
                <p>
                    <label for="SupervisorDropdown">Employed Status:</label>                    
                    <asp:DropDownList ID="EmployedStatusDropDown" runat="server">                        
                    </asp:DropDownList>
                </p>
            </fieldset>
        </div>
        <div class="govInfo">
                
            <fieldset class="registerEmployee">
                <legend>Information</legend>
                <p>
                    <label for="SSSno">SSS:</label>
                    <input type="text" id="SSSno" CssClass="textEntry" value="<%=Model.SSS %>"  />
                </p>
                <p>
                    <label for="PHno">PhilHealth:</label>
                    <input type="text" id="PHno" CssClass="textEntry" value="<%=Model.PhilHealth %>" />
                </p>
                <p>
                    <label for="TINno">TIN:</label>
                    <input type="text" id="TINno" CssClass="textEntry" value="<%=Model.Tin %>" />
                </p>
                <p>
                    <label for="Pagibigno">Pag-ibig:</label>
                    <input type="text" id="Pagibigno" CssClass="textEntry" value="<%=Model.PagIbig %>" />
                </p>
            </fieldset>
        </div>
        <div class="educationInfo">
                
            <fieldset class="registerEmployee">
                <legend>Information</legend>
                <p>
                    <label for="School1">Primary School Name / Period:</label>
                    <input type="text" id="School1" class="textEntrySchool" value="<%=Model.PrimarySchoolName %>" /> /
                    <input type="text" id="SchoolPeriod1" class="textEntryPeriod" value="<%=Model.PrimarySchoolPeriod %>" />
                </p>
                <p>
                    <label for="School2">Secondary School Name / Period:</label>
                    <input type="text" id="School2" class="textEntrySchool" value="<%=Model.SecondarySchoolName %>" /> /
                    <input type="text" id="SchoolPeriod2" class="textEntryPeriod" value="<%=Model.SecondarySchoolPeriod %>" />
                </p>
                <p>
                    <label for="School3">Tertiary School Name / Period:</label>
                    <input type="text" id="School3" class="textEntrySchool" value="<%=Model.TertiarySchoolPeriod %>" /> /
                    <input type="text" id="SchoolPeriod3" class="textEntryPeriod" value="<%=Model.TertiarySchoolPeriod %>" />
                </p>
                <p>
                    <label for="School3">College Degree:</label>
                    <input type="text" id="CollegeDegree" class="textEntry" value="<%=Model.CollegeDegree %>" />                    
                </p>
            </fieldset>
        </div>        
        <div class="personalInfo">
            <fieldset class="registerEmployee">
                <legend>Personal Information</legend>
                <p class="horizontal">
                    <label for="LastName">LastName:</label>
                    <input type="text" id="LastName" name="LastName" class="textEntryPersonal"  value="<%=Model.LastName %>" />
                </p>
                <p class="horizontal">
                    <label for="FirstName">FirstName:</label>
                    <input type="text" id="FirstName" name="FirstName" class="textEntryPersonal" value="<%=Model.FirstName %>" />
                </p>
                <p class="horizontal">
                    <label for="MiddleName">MiddleName:</label>
                    <input type="text" id="MiddleName" name="MiddleName" class="textEntryPersonal" value="<%=Model.MiddleName %>" />
                </p>
                <p class="clear"></p>
                <p class="horizontal">
                    <label for="NickName">NickName:</label>
                    <input type="text" id="NickName" class="textEntryPersonalShort" value="<%=Model.NickName %>" />
                </p>
                <p class="horizontal">
                    <label for="BirthDay">Date of Birth:</label>
                    <input type="text" id="BirthDay"  value="<%=Model.DateOfBirth.HasValue ?  Model.DateOfBirth.Value.ToShortDateString() : "" %>" class="textEntryPeriod" />
                    <input type="hidden" id="DateOfBirth" value="<%=Model.DateOfBirth.HasValue ?  Model.DateOfBirth.Value.ToShortDateString() : "" %>" />
                </p>
                <p class="horizontal">
                    <label for="PlaceOfBirth">Place of Birth:</label>
                    <input type="text" id="PlaceOfBirth"  value="<%=Model.PlaceOfBirth %>" class="textEntryPersonalShort" value="<%=Model.PlaceOfBirth %>" />
                </p>
                <p class="horizontal">
                    <label for="Religion">Religion:</label>
                    <input type="text" id="Religion"  value="<%=Model.Religion %>" class="textEntryPersonalShort" value="<%=Model.Religion %>" />
                </p>
                <p class="horizontal">
                    <label for="Qualifier">Qualifier:</label>
                    <input type="text" id="Qualifier"  class="textEntryPersonalShort" value="<%=Model.Qualifier %>" />
                </p>
                <p class="horizontal">
                    <label for="Height">Height:</label>
                    <input type="text" id="Height" class="textEntryPersonalShort" value="<%=Model.Height %>" />
                </p>
                <p class="horizontal">
                    <label for="Weight">Weight:</label>
                    <input type="text" id="Weight"  class="textEntryPeriod" value="<%=Model.Weight %>" />
                </p>
                <p class="horizontal">
                    <label for="Gender">Gender:</label>
                    <select id="Gender" style="width:146px">
                        <option value=""></option>
                        <option value="M" <%=Model.Gender == "M" ? "selected=\"selected\"" : ""  %> >Male</option>
                        <option value="F" <%=Model.Gender == "F" ? "selected=\"selected\"" : ""  %> >Female</option>
                    </select>
                </p>
                <p class="horizontal">
                    <label for="CivilStatus">Civil Status:</label>
                    <select id="CivilStatus" style="width:146px">
                        <option value=""></option>
                        <option value="S" <%=Model.CivilStatus == "S" ? "selected=\"selected\"" : ""  %> >Single</option>
                        <option value="M" <%=Model.CivilStatus == "M" ? "selected=\"selected\"" : ""  %>>Married</option>
                    </select>
                </p>
                <p class="horizontal">
                    <label for="YearsMarried">Years Married:</label>
                    <input type="text" id="YearsMarried"  class="textEntryPeriod" value="<%=Model.YearsMarried %>" />
                </p>
                <p class="horizontal">
                    <label for="ATMno">ATM no:</label>
                    <input type="text" id="ATMno"  class="textEntryPeriod" value="<%=Model.ATMno %>" />
                </p>
            </fieldset>
            <p class="submitButton">                
                <input type="button" id="EmployeeSubmitButton" value="<%=Model.Id > 0 ? "Update" : "Submit" %>" />
                <input type="button" id="btnClear" value="Clear Form" />                
                    <asp:Button ID="btnBack" runat="server" Text="Back to list" 
                    onclick="btnBack_Click" />
            </p>
        </div>                
        